package com.eightywork.life.fragment;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.SupportMapFragment;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.eightywork.life.R;
import com.eightywork.life.adapter.MapMarkerInfoAdapter;
import com.eightywork.life.widget.ActionsContentView;
import com.umeng.analytics.MobclickAgent;

public class MapFragment extends BaseFragment implements OnClickListener,
		AMapLocationListener {
	public static final String TAG = MapFragment.class.getName();
	private ActionsContentView viewActionsContentView;
	private ImageButton leftButton, rightButton;
	private Activity context;
	public LayoutInflater inflater;
	private AMap aMap;
	private LocationManagerProxy mAMapLocManager = null;
	private String longitude, latitude, name;

	public MapFragment(Activity context,
			ActionsContentView viewActionsContentView) {
		this.context = context;
		this.viewActionsContentView = viewActionsContentView;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.inflater = inflater;
		return inflater.inflate(R.layout.fragment_map, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		init();
		super.onActivityCreated(savedInstanceState);
	}

	private void init() {
		name = getActivity().getIntent().getStringExtra("name");
		longitude = getActivity().getIntent().getStringExtra("longitude");
		latitude = getActivity().getIntent().getStringExtra("latitude");
		initHeader(name, viewActionsContentView);
		leftButton = (ImageButton) getView().findViewById(R.id.button_left);
		rightButton = (ImageButton) getView().findViewById(R.id.button_right);
		leftButton.setOnClickListener(this);
		rightButton.setOnClickListener(this);
		aMap = ((SupportMapFragment) getFragmentManager().findFragmentById(
				R.id.map)).getMap();
		LatLng latlng = new LatLng(Double.valueOf(latitude),
				Double.valueOf(longitude));
		aMap.addMarker(new MarkerOptions()
				.position(latlng)
				.title("1")
				.icon(BitmapDescriptorFactory
						.defaultMarker(BitmapDescriptorFactory.HUE_RED))
				.draggable(true));
		aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));
		// aMap.setOnMarkerClickListener(new OnMarkerClickListener() {
		// @Override
		// public boolean onMarkerClick(Marker marker) {
		// return false;
		// }
		// });
		aMap.setInfoWindowAdapter(new MapMarkerInfoAdapter(context));
		mAMapLocManager = LocationManagerProxy.getInstance(context);
	}

	@Override
	public void onClick(View v) {
		if (v == leftButton) {
			if (viewActionsContentView.isActionsShown()) {
				viewActionsContentView.showContent();
			} else {
				viewActionsContentView.showActions();
			}
		} else if (v == rightButton) {
			// Intent intent = new Intent(context, IndexActivity.class);
			// intent.putExtra("default", NeighbourFragment.TAG);
			// intent.putExtra("position", 1);
			// context.startActivity(intent);
			// Activity activity = (Activity) context;
			// activity.finish();
		}
	}

	public void enableMyLocation() {
		// Location API定位采用GPS和网络混合定位方式，时间最短是5000毫秒
		mAMapLocManager.requestLocationUpdates(
				LocationProviderProxy.AMapNetwork, 5000, 10, this);
	}

	public void disableMyLocation() {
		if (mAMapLocManager != null) {
			mAMapLocManager.removeUpdates(this);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getActivity());
		enableMyLocation();
	}

	@Override
	public void onPause() {
		disableMyLocation();
		super.onPause();
		MobclickAgent.onPause(getActivity());
	}

	@Override
	public void onDestroy() {
		if (mAMapLocManager != null) {
			mAMapLocManager.removeUpdates(this);
			mAMapLocManager.destory();
		}
		mAMapLocManager = null;
		super.onDestroy();
	}

	@Override
	public void onLocationChanged(Location location) {
	}

	@Override
	public void onProviderDisabled(String provider) {

	}

	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	@Override
	public void onLocationChanged(AMapLocation location) {
		if (location != null) {
			// if (!isGotLocation) {
			// Double geoLat = location.getLatitude();
			// Double geoLng = location.getLongitude();
			// aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
			// new LatLng(geoLat, geoLng), 15));
			// isGotLocation = true;
			// }
		}
	}
}
